package com.wyclabs.cardmaker;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;

import com.jfoenix.controls.JFXToggleButton;
import com.rnkrsoft.bopomofo4j.Bopomofo4j;
import com.wyclabs.cardmaker.constants.CardType;
import javafx.beans.property.SimpleStringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

/**
 * @author TylerW
 * @date 06/22/21 11:18 AM
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SettingController extends CommonController implements Initializable {

    public static final String name = "Setting";

    @FXML
    private TextField creaturePrefix;

    @FXML
    private TextField heroPrefix;

    @FXML
    private TextField magicPrefix;

    @FXML
    private TextField baseCard;

    @FXML
    private TextField baseHero;

    @FXML
    private JFXToggleButton autoName;

    @SneakyThrows
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        CommonService.controllers.put(name, this);
        Properties prop = CommonService.config;
        creaturePrefix.setText(prop.getProperty("creature_prefix"));
        heroPrefix.setText(prop.getProperty("hero_prefix"));
        magicPrefix.setText(prop.getProperty("magic_prefix"));
        baseCard.setText(prop.getProperty("base_card"));
        baseHero.setText(prop.getProperty("base_hero"));
        autoName.setSelected(Boolean.parseBoolean(prop.getProperty("auto_name")));
    }

    @SneakyThrows
    public void save() {
        Properties prop = CommonService.config;
        prop.setProperty("creature_prefix", creaturePrefix.getText());
        prop.setProperty("hero_prefix", heroPrefix.getText());
        prop.setProperty("magic_prefix", magicPrefix.getText());
        prop.setProperty("base_card", baseCard.getText());
        prop.setProperty("base_hero", baseHero.getText());
        prop.setProperty("auto_name", String.valueOf(autoName.isSelected()));
        File propFile = CommonService.readResource("resource/config.properties");
        FileOutputStream fileOutputStream = new FileOutputStream(propFile);
        prop.store(fileOutputStream, "");
        getRootStage().hide();

        MainPaneController mainPaneController = (MainPaneController) CommonService.controllers.get(MainPaneController.name);
        for (Card item : mainPaneController.getTableView().getItems()) {
            if (StringUtils.isAnyBlank(item.getType().getValue(), item.getName().getValue())) {
                continue;
            }
            String name = item.getName().getValue();
            String cnName = null;
            if (autoName.isSelected()) {
                cnName = Bopomofo4j.pinyin(name, 2, false, false, "");
            }
            if (CardType.CREATURE.getType().equals(item.getType().getValue())) {
                item.setPath(new SimpleStringProperty(creaturePrefix.getText() + cnName));
            } else if (CardType.MAGIC.getType().equals(item.getType().getValue())) {
                item.setPath(new SimpleStringProperty(magicPrefix.getText() + cnName));
            } else {
                item.setPath(new SimpleStringProperty(heroPrefix.getText() + cnName));
            }
        }
        mainPaneController.getTableView().refresh();

    }
}
